package cn.jobstart.web.common;


import cn.jobstart.common.HttpTookit;
import cn.jobstart.common.NumberUtil;
import cn.jobstart.common.StringUtil;
import cn.jobstart.common.YXDate;
import cn.jobstart.common.redis.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Calendar;
import java.util.Date;

/**
 * @author sunke
 * @Date 2021/3/23 09:52
 * @description 将返回工作日（0）、休息日（1）和节假日（2）三种状态码：
 */

@Component
public class HolidayUtil {


    @Autowired
    private SkRestTemplate skRestTemplate;

    @Autowired
    private RedisUtil redisUtil;

    /**
     * 得到某个日期类型 是工作日（0）、休息日（1）和节假日（2）三种状态码：
     *
     * @param date
     * @return
     */
    public Integer getDayType(Date date) {

        if (date == null) {
            date = new Date();
        }
        String dateStr = YXDate.getFormatDate2String(date, "yyyyMMdd");
        Integer flag = getDayType(dateStr);
        if (flag == null || flag == -1) {
            return null;
        }
        return flag;
    }


    public Integer getDayType(String yyyyMMdd) {

        return getDayType(yyyyMMdd, false);

    }


    public Integer getDayType(String yyyyMMdd, boolean clear) {

        try {
            if (StringUtil.isEmpty(yyyyMMdd)) {
                return null;
            }

            if (!clear) {

                if (redisUtil.hasKey(yyyyMMdd)) {
                    return NumberUtil.parseInt(redisUtil.get(yyyyMMdd), -1);
                }
            }


            String url = "https://tool.bitefu.net/jiari/?d=" + yyyyMMdd;


            String res = HttpTookit.doGet(url, null);

            //说明取到数据
            if (StringUtil.isNotObjEmpty(res)) {

                Integer holidayFlag = NumberUtil.parseInt(res, -1);

                if (holidayFlag == -1) {
                    return null;
                }


                redisUtil.set(yyyyMMdd, holidayFlag, 60 * 24 * 60 * 60 * 1000);

                return holidayFlag;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;


    }

    public Date addWorksDate(Date currentDate, int days) {

        return addDate(currentDate, days, 1);


    }



    public Date addDay(Date currentDate,int days){
        return addDate(currentDate,days,0);
    }

    /**
     * 添加几工作日
     * 不包括当前天
     * @param currentDate 当前时间
     * @param days        几日
     * @param type        0=自然日  1：工作日
     * @return
     */

    public Date addDate(Date currentDate, Integer days, int type) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(currentDate);
        int i = 0;
        if(days==null){
            days=0;
        }

        if(type==0){
            return YXDate.addDay(currentDate,days);
        }


        while (i < days) {


            calendar.add(Calendar.DATE, 1);
            i++;

            Integer dayType = getDayType(calendar.getTime());

            if (type == 1) {
                if (dayType == null) {
                    if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY ||
                            calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
                        i--;
                    }
                } else {
                    if (dayType == 1 || dayType == 2) {

                        i--;
                    }

                }
            }
        }
        return calendar.getTime();
    }


}
